home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Justify Layout ƒ / Justify Layout.c next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  15.5 KB  |  472 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Justify Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "color library.c", "font library.c", "graphics debug library.c",
  11. |**|    "layout library.c", "shape library.c", and "transform library.c".
  12. |**|
  13. |**|    ©1992-1994  Apple Computer, Inc.
  14. |**|    All rights reserved.
  15. |**|
  16. |**| =====================================================================
  17. \**/
  18.  
  19.  
  20. #include "QDGX shell.h"
  21. #include "layout routines.h"
  22. #include "layout library.h"
  23.  
  24.  
  25. /**\
  26. |**| ---------------------------------------------------------------------
  27. |**| PROTOTYPES
  28. |**| ---------------------------------------------------------------------
  29. \**/
  30.  
  31. // funtions required by shell
  32.  
  33. void    DoSetup            (void);
  34. void    DoDraw            (WindowPtr wind, Boolean updating);
  35. OSErr    DoCreateNew        (void);
  36. void    DoDispose        (WindowPtr wind);
  37. void    DoIdle            (WindowPtr wind);
  38. void    DoTeardown        (void);
  39. void    DoClick            (WindowPtr wind, Point p);
  40.  
  41. // private functions
  42.  
  43. OSErr    DoWindowInit        (WindowPtr wind);
  44. void    CreateSampleImage    (WindowPtr wind);
  45.  
  46.  
  47. /**\
  48. |**| ---------------------------------------------------------------------
  49. |**| ENUMS
  50. |**| ---------------------------------------------------------------------
  51. \**/
  52. enum { rWindResource = 128 };
  53.  
  54.  
  55. /**\
  56. |**| ---------------------------------------------------------------------
  57. |**| GLOBALS
  58. |**| ---------------------------------------------------------------------
  59. \**/
  60. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  61. // functionality will only work with the "debugging" version of QuickDraw GX.
  62. // If the debugging version is not installed, nothing bad will happen, but these
  63. // functions will not work. 
  64.  
  65. Boolean        gDebugging = true;
  66.  
  67. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  68.  
  69. Boolean        gGiveMeValidation = true;
  70.  
  71.  
  72. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  73. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  74. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  75. // since we need abunch if we have several windows open.
  76.  
  77. long        gGraphicsHeapSize = 600;
  78.  
  79. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  80. // override.  This is so that our override can be native PowerPC code if necessary.
  81.  
  82. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  83.  
  84.  
  85.  
  86. /**\
  87. |**| ---------------------------------------------------------------------
  88. |**| DoSetup()
  89. |**| Here's where we initialize any global variables our application needs.
  90. |**| We have only one at this time -- the universal proc pointer for
  91. |**| our printing override.
  92. |**| ---------------------------------------------------------------------
  93. \**/
  94. void DoSetup (void)
  95. {    // Initialize our printing event override UPP
  96.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  97. }
  98.  
  99.  
  100. /**\
  101. |**| ---------------------------------------------------------------------
  102. |**| DoDraw()
  103. |**| Draw the contents of the window.  The first parameter is the window
  104. |**| to draw, and the second parameter is true if we're updating an existing
  105. |**| image.  If that's the case, we don't want to change anything, but
  106. |**| just draw what's already there.
  107. |**| ---------------------------------------------------------------------
  108. \**/
  109. void DoDraw (WindowPtr wind, Boolean updating)
  110. {
  111.      #pragma unused (updating)
  112.      GXDrawShape (GetDocShape(wind));
  113. }
  114.  
  115.  
  116. /**\
  117. |**| ---------------------------------------------------------------------
  118. |**| DoCreateNew()
  119. |**| This routine is called when a window needs to be created.
  120. |**| ---------------------------------------------------------------------
  121. \**/
  122. OSErr DoCreateNew (void)
  123. {
  124.     OSErr        err = noErr;
  125.     WindowPtr    wind;
  126.     
  127. // Get and create our window from the resource fork
  128.  
  129.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  130.  
  131. // Attach a default gxViewPort to it, create and iInitialize our
  132. // private data for it, and add a sample image to its page shape.
  133.  
  134.     if ( wind == NULL )
  135.         return (MemError());
  136.  
  137.     GXIgnoreGraphicsNotice(transform_already_set);
  138.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  139.     GXPopGraphicsNotice();
  140.     
  141.     err = DoWindowInit(wind);
  142.     if ( err != noErr )
  143.         return err;
  144.     
  145.     CreateSampleImage(wind);
  146.     return err;
  147. }
  148.  
  149.  
  150. /**\
  151. |**| ---------------------------------------------------------------------
  152. |**| DoDispose()
  153. |**| This routine is called when a window needs to be disposed of.
  154. |**| ---------------------------------------------------------------------
  155. \**/
  156. void DoDispose (WindowPtr wind)
  157. {
  158.     TH_Doc    doc;
  159.     
  160. // You should always dispose of your GX graphics objects before tossing your window.
  161. // Why?  It's generally good form and this approach guarantees that everything is
  162. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  163. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  164. // with notices set, you will receive a notice that you had not disposed of everything.
  165. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  166.     
  167.     if ( wind != NULL )
  168.     {
  169.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  170.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  171.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  172.         DisposHandle((Handle) doc);            // Dispose of our private data.
  173.         DisposeWindow(wind);                // Dispose of the window.
  174.     }
  175. }
  176.  
  177.  
  178. /**\
  179. |**| ---------------------------------------------------------------------
  180. |**| DoIdle()
  181. |**| This routine is called to do things while idling through the event loop.
  182. |**| ---------------------------------------------------------------------
  183. \**/
  184. void DoIdle (WindowPtr wind)
  185. {
  186. }
  187.  
  188.  
  189. /**\
  190. |**| ---------------------------------------------------------------------
  191. |**| DoTeardown()
  192. |**| This routine is called just before we quit to remove anything 
  193. |**| persistent that might have been setup by DoSetup().
  194. |**| ---------------------------------------------------------------------
  195. \**/
  196. void DoTeardown (void)
  197. {
  198.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  199. }
  200.  
  201.  
  202. /**\
  203. |**| ---------------------------------------------------------------------
  204. |**| DoClick()
  205. |**| ---------------------------------------------------------------------
  206. \**/
  207. void DoClick(WindowPtr window, Point p)
  208. {
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. /**\
  216. |**| ---------------------------------------------------------------------
  217. |**| DoWindowInit()
  218. |**| In this function we create and initialize the the private document
  219. |**| structure for a new window.  This structure contains the print job and
  220. |**| the shape which is drawn in the window.  We store this data in a handle
  221. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  222. |**| this, rather than using globals, we can create many windows containing
  223. |**| unique print jobs and shapes.
  224. |**| ---------------------------------------------------------------------
  225. \**/
  226. OSErr DoWindowInit (WindowPtr wind)
  227. {
  228.     OSErr    err = noErr;
  229.     gxJob    docJob;
  230.     gxShape    docPage;
  231.     TH_Doc    windDoc;
  232.  
  233.  
  234. // Create the page shape. We set the unique items attribute to make sure that each item
  235. // added to the picture has a unique reference. If this attribute was not set, we would
  236. // not see all copies of anything we add to the shape multiple times -- we'd just see
  237. // the last version added.        
  238.  
  239.     docPage = GXNewShape(gxPictureType);
  240.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  241.     
  242.     
  243. // Create a print job for this document.  This will be the same as the system default until
  244. // the user goes through the dialogs for Page Setup or Print…
  245.  
  246.     err = GXNewJob(&docJob);
  247.     
  248.     
  249. // If there are no errors, create a handle the size of our document structure and store
  250. // the print job and page shape in it.  Store the handle in the window's refCon field so
  251. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  252. // can be used to do this easily.
  253.  
  254.     if ( err == noErr )
  255.     {
  256.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  257.  
  258.         if ( windDoc == NULL )
  259.             err = MemError();
  260.         else
  261.         {
  262.             (*windDoc)->docJob = docJob;
  263.             (*windDoc)->docPage = docPage;
  264.             SetWRefCon(wind, (long) windDoc);
  265.         }
  266.  
  267. // Now install our application override for PrintingEvent so that we can
  268. // support the new movable-modal printing dialog boxes.
  269.  
  270.         GXInstallApplicationOverride(docJob, gxPrintingEvent, gOurPrintingOverrideUPP);
  271.  
  272.     }
  273.  
  274.     return err;
  275. }
  276.  
  277.  
  278. /**\
  279. |**| ---------------------------------------------------------------------
  280. |**| CreateSampleImage()
  281. |**| This function creates primitive shapes and adds them to the window's page shape.
  282. |**| ---------------------------------------------------------------------
  283. \**/
  284. void CreateSampleImage (WindowPtr wind)
  285. {
  286.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  287.                                             // in QuickDraw coordinates
  288.     gxShape layout1;                    // the first layout shape we build
  289.     gxShape layout2;                    // the second layout shape we build
  290.     gxShape layout3;                    // the third layout shape we build
  291.     gxShape layout4;                    // the fourth layout shape we build
  292.     gxShape thePage;                    // this window's document shape
  293.     gxPoint    posn;                        // position for our shapes
  294.     gxRunControls     runControls;        // run controls for the layout shape
  295.     gxLayoutOptions layoutOptions;        // options for the layout shape
  296.     StyleRunOverrides overrides;        // style run overrides for our shapes
  297.  
  298. // We use these two structures to assign all justification priority either
  299. // all to the space characters (allToSpace) or all to intercharacter spacing
  300. // (allToChar).
  301.  
  302.     gxPriorityJustificationOverride allToSpace, allToChar;
  303.                                         // 
  304.  
  305.     char *    text1 = "QuickDraw GX Line Layout is a set of support";
  306.     char *    text2 = "routines that applications call to implement";
  307.     char *    text3 = "certain functions. It is not a text editor or";
  308.     char *    text4 = "word processor, but it could help make one.";
  309.  
  310.     
  311. // make default gxLayoutOptions and gxRunControls structures.  These routines are
  312. // found in layout library.c.
  313.  
  314.     InitializeLayoutOptions (&layoutOptions);
  315.     InitializeRunControls (&runControls);
  316.     InitializeStyleRunOverrides(&overrides);
  317.     
  318. // Initialize default gxPriorityJustOverrides structures with another handy
  319. // routine found in layout library.c.
  320.  
  321.     SetDefaultPriorityJustOverride(&allToSpace);
  322.     SetDefaultPriorityJustOverride(&allToChar);
  323.     
  324. // For the allToSpace structure, we set the unlimited override flag as well as
  325. // unlimited gap absorption, which forces all extra width introduced during
  326. // justification to go to the white-space characters.
  327.  
  328.     allToSpace.deltas[gxWhiteSpacePriority].growFlags |= 
  329.         (gxOverrideUnlimited | gxUnlimitedGapAbsorption);
  330.  
  331. // For the allToChar structure, we set the unlimited override flag as well as
  332. // overriding the inter-character priority to "kashida," which is the highest
  333. // priority.  This guarantees that inter-character spacing is adjusted when
  334. // justifying a line.
  335.  
  336.     allToChar.deltas[gxInterCharPriority].growFlags |=
  337.         (gxOverrideUnlimited | gxUnlimitedGapAbsorption | 
  338.          gxOverridePriority | gxKashidaPriority);
  339.     
  340. // We set the text to about twice the width really needed
  341. // so that the justification effects are clearly visible.
  342.  
  343.     layoutOptions.width = ff(6.5 * 72);    
  344.     layoutOptions.just = fract1;            // full justification
  345.     
  346. // To position the lines, we compute the position that would center a 
  347. // layout's baseline in the window. We'll draw the lines 30 points apart,
  348. // so we'll put the first two 45 and 15 points above this position and the
  349. //    second two 15 and 45 points below it.
  350.  
  351.     posn.x = (ff(ourWindowRect.right - ourWindowRect.left) 
  352.                 - layoutOptions.width) / 2;
  353.     posn.y = ff((( ourWindowRect.bottom - ourWindowRect.top) / 2) - 45);
  354.  
  355.  
  356. // Setup complete!  Build the layouts, using NewSingleLayout from layout library.c
  357. // to create layouts with single style runs.  The first and second layouts have no
  358. // options, but the third one has all justification in spaces and the fourth has
  359. // all justification in inter-character spacing.
  360.  
  361.     GXIgnoreGraphicsNotice(text_size_already_set);
  362.     layout1 = NewSingleLayout(
  363.                 text1,                             // the text for the layout
  364.                 (char *) "\pHoefler Text",         // the font name to use
  365.                 ff(12),                         // the point size to use
  366.                 &layoutOptions,                 // our default layout options
  367.                 &posn,                             // the shape's position
  368.                 0,                                 // no text attributes
  369.                 &runControls,                    // our default run controls
  370.                 nil,                             // the run features array (none)
  371.                 0,                                 // count of zero features = 0
  372.                 nil);                            // no style run overrides
  373.     GXPopGraphicsNotice();
  374.     posn.y += ff(30);                            // move down 30 pixels
  375.     
  376.     GXIgnoreGraphicsNotice(text_size_already_set);
  377.     layout2 = NewSingleLayout(
  378.                 text2,                             // the text for the layout
  379.                 (char *) "\pHoefler Text",         // the font name to use
  380.                 ff(12),                         // the point size to use
  381.                 &layoutOptions,                 // our default layout options
  382.                 &posn,                             // the shape's position
  383.                 0,                                 // no text attributes
  384.                 &runControls,                    // our default run controls
  385.                 nil,                             // the run features array (none)
  386.                 0,                                 // count of zero features = 0
  387.                 nil);                            // no style run overrides
  388.     GXPopGraphicsNotice();
  389.  
  390.     posn.y += ff(30);                            // move down 30 pixels
  391.     overrides.priorityJustOverride = &allToSpace;
  392.         
  393.     GXIgnoreGraphicsNotice(text_size_already_set);
  394.     layout3 = NewSingleLayout(
  395.                 text3,                             // the text for the layout
  396.                 (char *) "\pHoefler Text",         // the font name to use
  397.                 ff(12),                         // the point size to use
  398.                 &layoutOptions,                 // our default layout options
  399.                 &posn,                             // the shape's position
  400.                 0,                                 // no text attributes
  401.                 &runControls,                    // our default run controls
  402.                 nil,                             // the run features array (none)
  403.                 0,                                 // count of zero features = 0
  404.                 &overrides);                    // our style run overrides
  405.     GXPopGraphicsNotice();
  406.  
  407.     posn.y += ff(30);                            // move down 30 pixels
  408.     overrides.priorityJustOverride = &allToChar;
  409.         
  410.     GXIgnoreGraphicsNotice(text_size_already_set);
  411.     layout4 = NewSingleLayout(
  412.                 text4,                             // the text for the layout
  413.                 (char *) "\pHoefler Text",         // the font name to use
  414.                 ff(12),                         // the point size to use
  415.                 &layoutOptions,                 // our default layout options
  416.                 &posn,                             // the shape's position
  417.                 0,                                 // no text attributes
  418.                 &runControls,                    // our default run controls
  419.                 nil,                             // the run features array (none)
  420.                 0,                                 // count of zero features = 0
  421.                 &overrides);                    // our style run overrides
  422.     GXPopGraphicsNotice();
  423.  
  424.     
  425. // Retrieve the page shape so we can add to it.
  426.  
  427.     thePage = GetDocShape(wind);
  428.     
  429. // Add the layouts to the window's picture shape.  AddToShape is in shape library.c.
  430.  
  431.     AddToShape(thePage, layout1);
  432.     AddToShape(thePage, layout2);
  433.     AddToShape(thePage, layout3);
  434.     AddToShape(thePage, layout4);
  435.  
  436. // Create two lines to show the margins of these lines, and add them to the picture
  437. // shape.
  438.  
  439.     {
  440.         gxLine    bound;                        // lines showing edges of layouts
  441.         gxShape    lineShape;                    // shape to hold our lines
  442.         
  443.         bound.first.x = posn.x;
  444.         bound.first.y = posn.y - ff(135);
  445.         bound.last.x = posn.x; 
  446.         bound.last.y = posn.y + ff(15);
  447.         lineShape = GXNewLine(&bound);
  448.         AddToShape(thePage, lineShape);
  449.         GXDisposeShape(lineShape);
  450.         
  451.         bound.first.x += layoutOptions.width;
  452.         bound.last.x += layoutOptions. width;
  453.         lineShape = GXNewLine(&bound);
  454.         AddToShape(thePage, lineShape);
  455.         GXDisposeShape(lineShape);
  456.     }
  457.  
  458. // Now that all shapes are added to the picture, dispose of them
  459.  
  460.     GXDisposeShape(layout1);
  461.     GXDisposeShape(layout2);
  462.     GXDisposeShape(layout3);
  463.     GXDisposeShape(layout4);
  464.     
  465.  
  466. // Invalidate the window's portRect so that everything gets updated.
  467.     
  468.     SetPort(wind);
  469.     InvalRect(&ourWindowRect);
  470. }
  471.  
  472.